package sk.fiit.lulco.grafy;

/**
 * serializer
 * 
 * @author	Michal Lulco <michal.lulco@gmail.com>
 */
public class Serializer {

	/**
	 * method for unserializing string to 2D array
	 * 
	 * @param	string
	 * @return	int[][]
	 */
	public int[][] unserialize(String string) {
		int[][] output = null;
		int mainIndex = 0;
		int subIndex = 0;
		int size = 0;
		string = string.replaceAll(";\\}\\}", "");
		String[] parts = string.split(":\\{i:|;\\}i:");		
		for (int i = 0; i < parts.length; i++) {
			if (i == 0) {
				String stringSize = parts[i].replaceAll("a:", "");
				size = Integer.parseInt(stringSize) + 1;
				output = new int[size][size];
			} else if (i % 2 == 0) {
				String[] abc = parts[i].split(";i:");
				for (int j = 0; j < abc.length; j++) {
					if (j % 2 == 0) {
						subIndex = Integer.parseInt(abc[j]);
					} else {
						output[mainIndex][subIndex] = Integer.parseInt(abc[j]);
					}
				}
			} else {
				String[] xyz = parts[i].split(";a:");
				mainIndex = Integer.parseInt(xyz[0]);
			}

		}
		/*
		for (int i = 0; i < output.length; i++) {
			for (int j = 0; j < output.length; j++) {
				System.out.print(output[i][j]);
			}
			System.out.println();
		}
		*/
		
		return output;
	}
	
}
